package com.liuguang.rabbitmq.handler;

import com.liuguang.common.pojo.MqData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.listener.api.RabbitListenerErrorHandler;
import org.springframework.amqp.rabbit.support.ListenerExecutionFailedException;
import org.springframework.stereotype.Component;
import org.springframework.util.ErrorHandler;

/**
 * @ClassName: MyMqExceptionHandler
 * @Description：这种通常是使用自动ack 才会配合使用。因为手动ack 会自己处理异常
 * @Author: wanghr
 * Date: 3:33 PM 2020/11/5
 * Version: 1.0
 */
@Component("myMqExceptionHandler")
public class MyMqExceptionHandler implements RabbitListenerErrorHandler {
    private static final Logger logger = LoggerFactory.getLogger(MyMqExceptionHandler.class);
    @Override
    public Object handleError(Message amqpMessage, org.springframework.messaging.Message<?> message, ListenerExecutionFailedException exception) throws Exception {
        logger.info("进入MyMqExceptionHandler");
        byte[] body = amqpMessage.getBody();
        String s = new String(body);
        System.out.println("s = " + s);
        Object payload = message.getPayload();
        if (payload instanceof MqData) {
            MqData mqData = (MqData) payload;
            System.out.println("22222"+mqData.getMsg());
        }
//        ErrorHandler
//        RabbitListenerContainerFactory
//        RabbitAdmin
        System.out.println(exception.getMessage());
        return "异常已经被处理了";
    }
}
